package com.hdw.auth.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hdw.auth.api.CoreAuthorizationService;
import com.hdw.auth.api.RolesService;
import com.hdw.auth.utils.Ognlutis;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hdw.auth.bean.vo.LayMsg;
import com.hdw.auth.bean.vo.Msg;
import com.hdw.auth.bean.model.SysRoles;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author GuoPeng
 * @Title: RolesService
 * @Description: 角色控制器
 * @date 2019/5/11 10:34
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags="角色管理")
@RequestMapping("/permission")
public class RolesController {

    @Resource
    private RolesService rolesService;

    @Resource
    private CoreAuthorizationService coreAuthorizationService;

    @RequestMapping("/UserSetRoles")
    @ApiOperation(value="为用户添加角色")
    public Msg  UserSerRoles(String LoginKey,String data,Integer id){
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            Gson gson = new Gson();
            Map map=  (Map)gson.fromJson(data, new TypeToken<Map>(){}.getRawType());
            return rolesService.UserSetRoles(map, id);
        } else {
            return Msg.fail();
        }
    }

    //获取所有角色
    @RequestMapping("/getRolesAll")
    public Msg  getRolesAll(String LoginKey,Integer id){
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            return rolesService.selectAll(id);
        } else {
            return Msg.fail();
        }
    }


    //以下为角色的增删改查
    @RequestMapping("/insertRoles")
    @ResponseBody
    public Msg  insertRoles(String LoginKey,String data){
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            Gson gson = new Gson();
            SysRoles sysRoles=  (SysRoles)gson.fromJson(data, new TypeToken<SysRoles>(){}.getRawType());
            try {
                return rolesService.insertRoles(sysRoles);
            }catch (Exception e){
                return Msg.fail().add("info", "角色名称已存在！");
            }

        } else {
            return Msg.fail();
        }
    }

    @RequestMapping("/getlistRoles")
    @ApiOperation(value="查询用户")
    public LayMsg getRoleslist(String LoginKey, Integer page, Integer limit) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
           return rolesService.getRolesList(page, limit);
        } else {
            return LayMsg.fail();
        }
    }

    @RequestMapping("/updateRoles")
    public Msg updateRoles(String LoginKey, String roles) {
        Gson gson = new Gson();
        SysRoles sysRoles = (SysRoles) gson.fromJson(roles, new TypeToken<SysRoles>() {
        }.getRawType());
        Msg msg = coreAuthorizationService.autLoginToken(LoginKey);
        if (msg.getCode() == 100) {
            return rolesService.updateRoles(sysRoles);
        } else {
            return msg;
        }
    }


    @RequestMapping("/RolesLock")
    public Msg RolesLock(String LoginKey, Integer id, Boolean lock) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            SysRoles sysRoles = new SysRoles();
            sysRoles.setId(id);
            sysRoles.setAvailable(lock);
            return rolesService.RolesLock(sysRoles);
        } else {
            return Msg.fail();
        }
    }

    @RequestMapping("/delectRoles")
    public Msg delectRoles(String LoginKey, String roles) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            Gson gson = new Gson();
            List<Map> maps = (List) gson.fromJson(roles, new TypeToken<List<Map>>() {
            }.getRawType());
            for (int i = 0; i < maps.size(); i++) {
                Integer id = Ognlutis.getNumber("id", maps.get(i)).intValue();
                Msg delectuser = rolesService.delectRoles(id);
            }
            return Msg.success();
        }
        return Msg.fail();
    }

}
